﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using eCommerce.core.Helpers;
using eCommerce.db.DataRespository;
using eCommerce.db.SQLDataAccess;

namespace eCommerce.db.DALRespository
{
    public class DiscountDal
    {
        #region DiscountDal private
        private static void SetInfo(IDataRecord reader, ref DiscountEntity o)
        {
            for (int i = 0; i < reader.FieldCount; i++)
            {
                PropertyInfo propertyInfo = DbCommon.GetPropertyInfo(o, reader.GetName(i));
                if (((propertyInfo != null) && (reader[i] != null)) && (reader[i] != DBNull.Value))
                    propertyInfo.SetValue(o, reader[i], null);
                else
                    o[reader.GetName(i)] = reader[i];
            }
        }

        private static void SetInfoList(ref SqlDataReader reader, ref List<DiscountEntity> advList)
        {
            while (reader.Read())
            {
                var o = new DiscountEntity();
                SetInfo(reader, ref o);
                advList.Add(o);
            }
            reader.Dispose();
        }
        #endregion

        public static List<DiscountEntity> Search(string startDate, string endDate, int pageIndex, int pageSize, ref int totalRecords)
        {
            var filter = "1=1";

            if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
                filter += string.Format(" AND CreatedOn Between '{0} 00:00:00' AND '{1} 23:59:59'", Utils.ValidStrDateTime(startDate), Utils.ValidStrDateTime(endDate));

            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var lstDiscount = new List<DiscountEntity>();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Action",
                                Constant.TableDiscount, "StartDate DESC", Constant.AllFieldList, filter, pageIndex, pageSize);

                SetInfoList(ref reader, ref lstDiscount);

                totalRecords = (int)SqlHelper.ExecuteScalar(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Action_Count",
                                        Constant.TableArticle, filter);

                return lstDiscount;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Discount_Action_Pagination: {0}", ex.Message));
            }
        }

        public static DiscountEntity GetById(int id)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var o = new DiscountEntity();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Discount_GetById", id);
                if (reader.Read())
                    SetInfo(reader, ref o);
                return o;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Discount_GetById: {0}", ex.Message));
            }
        }

        public static void Create(DiscountEntity o)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Discount_Create", o.TotalValue, o.DiscountPercent, o.StartDate, o.EndDate);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Discount_Create: {0}", ex.Message));
            }
        }

        public static void Update(DiscountEntity o)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Discount_Update", o.DiscountId, o.TotalValue, o.DiscountPercent, o.StartDate, o.EndDate);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Discount_Update: {0}", ex.Message));
            }
        }

        public static void Delete(int id)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Discount_Update", id);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Discount_Update: {0}", ex.Message));
            }
        }
    }
}
